package com.ruoyi.work.controller;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import com.ruoyi.work.domain.MapNodes;
import com.ruoyi.work.service.IMapNodesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 节点Controller
 * 
 * @author ruoyi
 * @date 2025-09-28
 */
@Tag(name = "节点")
@RestController
@RequestMapping("/work/nodes")
public class MapNodesController extends BaseController
{
    @Autowired
    private IMapNodesService mapNodesService;

    /**
     * 查询节点列表
     */
    @Operation(summary = "查询节点列表")
    @PreAuthorize("@ss.hasPermi('work:nodes:list')")
    @GetMapping("/list")
    public TableDataInfo list(MapNodes mapNodes)
    {
        startPage();
        List<MapNodes> list = mapNodesService.selectMapNodesList(mapNodes);
        return getDataTable(list);
    }

    /**
     * 导出节点列表
     */
    @Operation(summary = "导出节点列表")
    @PreAuthorize("@ss.hasPermi('work:nodes:export')")
    @Log(title = "节点", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MapNodes mapNodes)
    {
        List<MapNodes> list = mapNodesService.selectMapNodesList(mapNodes);
        ExcelUtil<MapNodes> util = new ExcelUtil<MapNodes>(MapNodes.class);
        util.exportExcel(response, list, "节点数据");
    }

    /**
     * 获取节点详细信息
     */
    @Operation(summary = "获取节点详细信息")
    @PreAuthorize("@ss.hasPermi('work:nodes:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(mapNodesService.selectMapNodesById(id));
    }

    /**
     * 新增节点
     */
    @Operation(summary = "新增节点")
    @PreAuthorize("@ss.hasPermi('work:nodes:add')")
    @Log(title = "节点", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MapNodes mapNodes)
    {
        return toAjax(mapNodesService.insertMapNodes(mapNodes));
    }

    /**
     * 修改节点
     */
    @Operation(summary = "修改节点")
    @PreAuthorize("@ss.hasPermi('work:nodes:edit')")
    @Log(title = "节点", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MapNodes mapNodes)
    {
        return toAjax(mapNodesService.updateMapNodes(mapNodes));
    }

    /**
     * 删除节点
     */
    @Operation(summary = "删除节点")
    @PreAuthorize("@ss.hasPermi('work:nodes:remove')")
    @Log(title = "节点", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mapNodesService.deleteMapNodesByIds(ids));
    }

    /**
     * 导入节点
     */
    @Operation(summary = "导入节点")
    @Log(title = "节点", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importData(@RequestBody Map<String,String> data) throws Exception
    {
        boolean updateSupport = true;
        String filePath = RuoYiConfig.getProfile()+data.get("path").toString().replaceAll("/profile","");
        String message = "导入失败";
        try (InputStream input = new FileInputStream(filePath)) {
            // 操作流
            ExcelUtil<MapNodes> util = new ExcelUtil<MapNodes>(MapNodes.class);
            List<MapNodes> mapNodesList = util.importExcel(input);
            message = mapNodesService.importMapNodes(mapNodesList, updateSupport);
        }catch (Exception e) {
            message = "打开上传文件失败";
        }
        
        return AjaxResult.success(message);
    }
}
